<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <link rel="icon" href="http://5b0988e595225.cdn.sohucs.com/images/20171210/362dcd1c009842ff99b33f5d51bbfb80.jpeg" type="image/x-icon"/>
    <title>WebSocket Chat</title>
</head>
<body>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
    var socket;
    function getSocketCon() {
        if (window.WebSocket) {
            socket = new WebSocket("ws://39.98.135.214:8081/ws");
            socket.onmessage = function (event) {
                var ta = document.getElementById('responseText');
                ta.value = ta.value + '\n' + event.data
            };
            socket.onopen = function (event) {
                var ta = document.getElementById('responseText');
                ta.value = "连接开启!";
                var messageJson = {};
                messageJson.sendUserId = document.getElementById('userId').value;
                socket.send(JSON.stringify(messageJson));
            };
            socket.onclose = function (event) {
                var ta = document.getElementById('responseText');
                ta.value = ta.value + "连接被关闭";
            };
        } else {
            alert("你的浏览器不支持 WebSocket！");
        }
    }

    function send(message) {
        if (!window.WebSocket) {
            return;
        }
        var messageJson = {};
        messageJson.sendUserId = document.getElementById('userId').value;
        messageJson.receiveUserId = document.getElementById('receiveUserId').value;
        messageJson.messageType = 1;
        messageJson.content=message;
        if (socket.readyState == WebSocket.OPEN) {
            socket.send(JSON.stringify(messageJson));
        } else {
            alert("连接没有开启.");
        }
    }
    function login() {
        $.ajax({
            url:'/user/login',
            type:'post',
            dataType:'json',
            contentType:"application/x-www-form-urlencoded",
            async:true,//异步请求
            cache:false,
            data:$('#loginForm').serialize(),
            //执行成功的回调函数
            success:function(data) {
                javascript:document.getElementById('userId').value=data.data;
                getSocketCon();
            },
            //执行失败或错误的回调函数
            error:function(data) {
                alert(data.message);
            }
        });
    }
    function talkWith(id,name) {
        document.getElementById('receiveUserId').value=id;
        document.getElementById('talkName').value=name;
    }
</script>
<form id="loginForm">
    <input id="phone" name="phone" value="15001241944"/>用户名
    <input id="passWord" name="passWord" value="123456"/>密码
    <input type="button" value="登录" onclick="login()">
</form>
<br>
<br>
<table>
    <tr>
        <th>序号</th>
        <th>用户名</th>
        <th>手机号</th>
        <th>操作</th>
    </tr>
    <tr th:each="user:${userlist}">
        <td th:text="${user.id}"></td>
        <td th:text="${user.userName}"></td>
        <td th:text="${user.phone}"></td>
        <td>
            <button th:onclick="talkWith([[${user.id}]],[[${user.userName}]]);">聊天</button>
        </td>
    </tr>
</table>
<br>
<br>
<h3>聊天对象：</h3>
<input id="talkName"/>
<form onsubmit="return false;">
    <textarea id="responseText" style="width: 500px; height: 300px;"></textarea>
    <br>
    <textarea type="text" name="message" style="width: 500px; height: 100px;" value="hello"></textarea>
    <input type="button" value="发送消息" onclick="send(this.form.message.value)">
    <input type="button" onclick="javascript:document.getElementById('responseText').value=''" value="清空聊天记录">
    <input id="userId"/>
    <input id="receiveUserId" th:hidden="hidden"/>
</form>
<br>
<br>
</body>
</html>
